package com.atguigu.gulimall.coupon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.common.vo.SeckillSessionWithRelationSkuVo;
import com.atguigu.common.vo.SeckillSkuRelationVo;
import com.atguigu.gulimall.coupon.dao.SeckillSessionDao;
import com.atguigu.gulimall.coupon.entity.SeckillSessionEntity;
import com.atguigu.gulimall.coupon.entity.SeckillSkuRelationEntity;
import com.atguigu.gulimall.coupon.service.SeckillSessionService;
import com.atguigu.gulimall.coupon.service.SeckillSkuRelationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Resource
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询最近三天的所有秒杀活动及其涉及的所有商品
     * @return
     */
    @Override
    public List<SeckillSessionWithRelationSkuVo> getLatest3DaysSession() {
        // SELECT * FROM sms_seckill_session WHERE start_time BETWEEN '2022-03-01 00:00:00' AND '2022-03-03 23:59:59'
        List<SeckillSessionEntity> list = this.list(new LambdaQueryWrapper<SeckillSessionEntity>().between(SeckillSessionEntity::getStartTime, getStartTime(), getEndTime()));
        List<SeckillSessionWithRelationSkuVo> resultList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(list)) {
            resultList = list.stream().map(item -> {
                SeckillSessionWithRelationSkuVo vo = new SeckillSessionWithRelationSkuVo();
                BeanUtil.copyProperties(item, vo);
                Long sessionId = item.getId();
                List<SeckillSkuRelationEntity> relationSkus = seckillSkuRelationService.list(new LambdaQueryWrapper<SeckillSkuRelationEntity>().eq(SeckillSkuRelationEntity::getPromotionSessionId, sessionId));
                List<SeckillSkuRelationVo> skuRelationVos = new ArrayList<>();
                if (CollectionUtil.isNotEmpty(relationSkus)) {
                    skuRelationVos = relationSkus.stream().map(skuRelation -> {
                        SeckillSkuRelationVo skuRelationVo = new SeckillSkuRelationVo();
                        BeanUtil.copyProperties(skuRelation, skuRelationVo);
                        return skuRelationVo;
                    }).collect(Collectors.toList());
                }
                vo.setRelationSkus(skuRelationVos);
                return vo;
            }).collect(Collectors.toList());
        }
        return resultList;
    }

    /**
     * 获取起始日期时间
     * @return
     */
    private String getStartTime() {
        LocalDate today = LocalDate.now();
        LocalDateTime startTime = LocalDateTime.of(today, LocalTime.MIN);
        return startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 获取结束日期时间
     * @return
     */
    private String getEndTime() {
        LocalDate today = LocalDate.now();
        LocalDate afterTomorrow = today.plusDays(2);
        LocalDateTime endTime = LocalDateTime.of(afterTomorrow, LocalTime.MAX);
        return endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

}